import { Component } from '@angular/core'; 
import { ActivatedRoute, Router } from '@angular/router';
import { SidebarCmpComponent } from "../../component/sidebar-cmp/sidebar-cmp.component";
import { ContentCmpComponent } from "../../component/content-cmp/content-cmp.component";
import { MenubarCmpComponent } from "../../component/menubar-cmp/menubar-cmp.component";
import { ToolbarCmpComponent } from "../../component/toolbar-cmp/toolbar-cmp.component";
import { TitlebarCmpComponent } from "../../component/titlebar-cmp/titlebar-cmp.component";
import { InfobarCmpComponent } from "../../component/infobar-cmp/infobar-cmp.component";
import { FolderDetailsDto } from '../../dto/folderDetailsDto';
import { folder } from 'ispace.core.main';
import { CoreService } from '../../service/core.service';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import {MatDividerModule} from '@angular/material/divider';

@Component({
  selector: 'app-home-page',
  standalone: true,
  imports: [SidebarCmpComponent, ContentCmpComponent, MenubarCmpComponent, ToolbarCmpComponent, TitlebarCmpComponent, InfobarCmpComponent,MatDividerModule],
  templateUrl: './home-page.component.html',
  styleUrl: './home-page.component.scss'
})
export class HomePageComponent {

  constructor(private router: Router,private route: ActivatedRoute,private coreSvc:CoreService) {
 
  }

  folderData?:FolderDetailsDto; 
  folderPath = "";
  query = ""; 
  isInit = true;
 
  ngOnInit(): void {

    // step 1: 获取url参数路径  
    const sb = this.route.queryParams.subscribe(queryParams => {
    
      let type = queryParams['type'];
      let path = decodeURIComponent(queryParams['path']);
      if(type == 'folder') { 
        this. folderPath = path;
      }
      else if(type == 'file') {
        let datas = path.split("/");
        this.folderPath = datas.slice(0, datas.length - 1).join("/");
        this.query = datas[datas.length - 1];
      }

      if(this.folderPath == null || this.folderPath == "") {
        this.folderPath = "/";
      }

      // step 2: 获取文件夹详情 
      this.setCurrentFolder(this.folderPath);  
   
    }); 
    
  }

   setCurrentFolder(path:string) {
    
    // step 1: 获取文件夹信息
    folder.statf(path).subscribe({
      next: (s) => {
        this.folderData = s;
        this.folderPath = path;
      }, error: (e: any) => {
        console.log(e);
      }
    }); 

    if(this.isInit){
      this.isInit = false;
    this.coreSvc.init(path,this.query);
    }
    else{
      this.coreSvc.setPath(path);
      this.coreSvc.setQuery(this.query);
    }
    
  }
 

}
